package cn.tedu.csmall.product.service.impl;

import cn.tedu.csmall.commons.ex.ServiceException;
import cn.tedu.csmall.commons.pojo.vo.PageData;
import cn.tedu.csmall.commons.util.PageInfoToPageDataConverter;
import cn.tedu.csmall.commons.web.ServiceCode;
import cn.tedu.csmall.product.mapper.AlbumMapper;
import cn.tedu.csmall.product.mapper.PictureMapper;
import cn.tedu.csmall.product.pojo.entity.Picture;
import cn.tedu.csmall.product.pojo.param.PictureAddNewParam;
import cn.tedu.csmall.product.pojo.vo.AlbumStandardVO;
import cn.tedu.csmall.product.pojo.vo.PictureListItemVO;
import cn.tedu.csmall.product.pojo.vo.PictureStandardVO;
import cn.tedu.csmall.product.service.IPictureService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class PictureServiceImpl implements IPictureService {

    @Value("${csmall.crud.default-query-page-size}")
    private Integer defaultQueryPageSize;

   @Autowired
    private PictureMapper pictureMapper;

   @Autowired
    private AlbumMapper albumMapper;


    @Override
    public PageData<PictureListItemVO> listByAlbumId(Long albumId, Integer pageNum) {
        log.debug("开始处理【根据相册ID查询图片数据列表】的业务，相册：{}, 页码：{}", albumId, pageNum);
        PageData<PictureListItemVO> pageData = pictureMapper.listByAlbumId(albumId, pageNum, defaultQueryPageSize);
        return pageData;    }

    @Override
    public PageData<PictureListItemVO> listByAlbumId(Long albumId, Integer pageNum, Integer pageSize) {
        log.debug("开始处理【根据相册ID查询图片数据列表】的业务，相册：{}, 页码：{}，每页记录数：{}", albumId, pageNum, pageSize);
        PageData<PictureListItemVO> pageData = pictureMapper.listByAlbumId(albumId, pageNum, pageSize);
        return pageData;
    }

    @Override
    public void setCover(Long id) {

    }

    @Override
    public void delete(Long id) {

    }



    @Override
    public void addNew(PictureAddNewParam pictureAddNewParam) {
        log.info("PictureServiceImpl.addNew开始执行");
        //检查图片名称是否被占用，如果被占用，则抛出异常
        Long albumId = pictureAddNewParam.getAlbumId();
        AlbumStandardVO album = albumMapper.getStandardById(albumId);
        //判断图片所属相册是否存在

        if (album == null){
            //是，相册名称已经存在，抛出RuntimeException异常
            String message =  "添加图片失败！相册不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
        }


        //将图片数据写入到数据库中
        Picture picture = new Picture();
        BeanUtils.copyProperties(pictureAddNewParam, picture);
        picture.setIsCover(0);

        log.debug("准备将新的图片数据写入到数据库，数据：{}",picture);
        pictureMapper.insert(picture);
        log.debug("将新的图片数据写入到数据库，完成！");




    }

    @Override
    public PictureStandardVO getStandardById(Long id) {
        log.debug("开始处理[根据ID图片详情]的业务，参数:{}",id);
        PictureStandardVO queryResult = pictureMapper.getStandardById(id);
        if(queryResult == null){
            String message = "查询图片详情失败，图片数据不存在";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
        }
        return queryResult;
    }



}
